<template>
	<view class="">
		<view v-if="current==0 || current==1">
			<view class="card c3 mb10 cshow" v-for="item in orderList" :key="item.id">
				<view class="content">
					<view style="display: flex;justify-content: space-between;align-items: center;">
						<view class="f18 wei6">#{{item.order_no}}</view>
						<view class="wei f14">￥{{item.total_amount}}</view>
					</view>
					<u-divider></u-divider>
					<view class="center">
						<view class="order-name">{{item.product_name}}</view>
						<view class="order-info">
							<span style="margin-right: 10rpx;">门店：{{item.store_name}} </span>
							<u-tag type="error" v-if="item.other.length == 0 && item.is_save == 0" text="外卖单" plain
								size="mini"></u-tag>
							<u-tag type="error" v-if="item.is_save == 1" text="先囤单" plain size="mini"></u-tag>
							<u-tag type="error" v-if="item.is_save == 2" text="后约单" plain size="mini"></u-tag>
							<u-tag v-if="item.other.length != 0" text="购物车" plain size="mini"></u-tag>
							<u-tag type="success" v-if="item.is_group != 0" text="团购单" plain size="mini"></u-tag>
							<u-tag type="success" v-if="item.is_delivery != 0" text="已存在后约单" plain size="mini"></u-tag>
						</view>
						<view class="order-info">
							<span>下单时间：{{item.create_time}}</span>
						</view>
						<view class="order-info"><span>订单状态：</span>
							<u-tag v-if="item.order_status == 0" type="warning" text="未知" size="mini" plain
								class="tag"></u-tag>
							<u-tag v-if="item.order_status == 1" text="待支付" size="mini" plain class="tag"></u-tag>
							<u-tag v-if="item.order_status == 2" type="error" text="已取消" size="mini" plain
								class="tag"></u-tag>
							<u-tag v-if="item.order_status == 3" type="warning" text="待使用" size="mini" plain
								class="tag"></u-tag>
							<u-tag v-if="item.order_status == 4" type="success" text="已完成" size="mini" plain
								class="tag"></u-tag>
							<u-tag v-if="item.order_status == 5" type="warning" text="退款中" size="mini" plain
								class="tag">
							</u-tag>
							<u-tag v-if="item.order_status == 6" type="success" text="已退款" size="mini" plain
								class="tag"></u-tag>
							<u-tag v-if="item.order_status == 7" text="支付成功" size="mini" plain class="tag"></u-tag>
						</view>
						<view class="order-info"><span style="margin-right: 20rpx;">退款状态:</span>
							<u-tag class="tag" v-if="item.return_status == 0" type="warning" text="未申请" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 1" type="error" text="已申请中" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 2" type="success" text="同意" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 3" type="warning" text="拒绝" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 4" type="error" text="退款成功" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 5" type="success" text="退款中" plain
								size="mini"></u-tag>
							<u-tag class="tag" v-if="item.return_status == 6" type="warning" text="退款失败" plain
								size="mini"></u-tag>
						</view>
						<view class="order-info"><span style="margin-right: 20rpx;">配送状态:</span>
							<u-tag class="tag" v-if="item.delivery_status == 0" type="warning" size="mini" plain
								text="未确认"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 1" type="error" size="mini" plain
								text="已确认"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 2" type="success" size="mini" plain
								text="已出餐"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status === 3" type="success" size="mini" plain
								text="退款完成"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status === 4" type="error" size="mini" plain
								text="退款失败"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 5" type="warning" size="mini" plain
								text="已配送"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 6" type="success" size="mini" plain
								text="配送完成"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 7" type="error" size="mini" plain
								text="配送取消"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 8" type="warning" size="mini" plain
								text="配送退回"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 9" type="error" size="mini" plain
								text="配送转单"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 10" type="success" size="mini" plain
								text="骑手已接单"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 11" type="error" size="mini" plain
								text="骑手已到店"></u-tag>
							<u-tag class="tag" v-if="item.delivery_status == 12" type="warning" size="mini" plain
								text="待骑手接单"></u-tag>
						</view>
					</view>
					<u-collapse :border="false" v-if="item.order_status != 2">
						<u-collapse-item title="更新状态" name="Docs guide" icon="setting">
							<view class="setting">
								<view class="detail" @click="editStatus(1,item)">接单</view>
								<view class="detail" @click="editStatus(300,item)">已配送</view>
								<view class="detail" @click="editStatus(500,item)">配送完成</view>
							</view>
						</u-collapse-item>
					</u-collapse>
					<u-divider></u-divider>
					<view class="footer">
						<view class="detail" @click="detail(item)" v-if="menus.includes('order/order/info')">详情</view>
						<view class="detail"
							v-if="menus.includes('order/order/dy_create_refund') && item.return_status != 4 &&item.return_status != 5 &&item.delivery_status != 6 &&(item.order_status === 7 || item.order_status === 5)"
							@click="yijiantuikuan(item.id)">一键退款</view>
						<view class="detail"
							v-if="item.delivery_status === 6 && item.return_status != 4 && menus.includes('order/order/dy_create_refund')"
							@click="yijiantuikuan(item.id)">一键退款</view>
						<view class="detail" @click="refuseBtn(item.id)" v-if="item.return_status == 1 && item.is_save != 1  &&
							menus.includes('order/order/return_refuse')">拒绝退款</view>
						<view class="detail" @click="agreeBtn(item.id)"
							v-if="item.return_status == 1 && item.is_save != 1 && menus.includes('order/order/return_agree')">
							同意退款</view>
						<view class="detail" @click="dakai(item)"
							v-if="item.order_status != 0 &&item.order_status != 1 &&item.order_status != 2 && menus.includes('order/order/chaxunfenzhang')">
							查询分账</view>
						<view class="detail" @click="jiesuan(item)"
							v-if="item.delivery_status === 6 && (item.return_status === 0 || item.return_status === 6) && menus.includes('order/order/shoudongjieshuan')">
							手动结算</view>
					</view>
				</view>
			</view>
		</view>
		<u-modal :show="dialogVisibleTi" :title="title" :showConfirmButton="false" :closeOnClickOverlay="true"
			@close="dialogVisibleTi=false">
			<view class="slot-content" v-for="item in tableDataTi" :key="item.id">
				<view class="">
					订单号：{{item.out_order_no}}
				</view>
				<view class="">
					订单id：{{item.order_id}}
				</view>
				<view class="">
					结算总金额：{{item.settle_amount/100}}
				</view>
				<view class="">
					结算单号：{{item.out_settle_no}}
				</view>
				<view class="">
					抖音结算单号：{{item.settle_id}}
				</view>
				<view class="">
					抖音结算明细：{{item.settle_detail}}
				</view>
				<view class="" style="display: flex;align-items: center;">
					结算状态：
					<u-tag :type="settle_statusList[item.settle_status].type" size="mini" plain
						:text="settle_statusList[item.settle_status].name"></u-tag>
				</view>
				<view class="">
					结算时间： {{ formatDate(item.settle_time) }}
				</view>
				<u-divider></u-divider>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex'
	export default {
		props: {
			orderList: {
				type: Array,
				default () {
					return []
				}
			},
			current: {
				type: Number,
				default: 0
			},
			parentParam: {
				limit: 10,
				status: 0,
				order_status: 0,
				page: 1,
				product_name: '',
			}
		},
		data() {
			return {
				newPageUrl: this.api.dy_create_refund,
				subColor: uni.getStorageSync('subject_color'),
				imageUrl: 'https://life.kdsh.cc/',
				params: {
					order_id: '',
				},
				type: 0,
				id: '',
				order_id: '',
				return_refuse_msg: '',
				refuseShow: false,
				inputVal: '',
				parentParam2: this.parentParam,
				dialogVisibleTi: false,
				tableDataTi: [],
				settle_statusList: {
					FAIL: {
						type: 'error',
						name: '结算失败',
					},
					SUCCESS: {
						type: 'success',
						name: '已结算',
					},
				}
			}
		},
		computed: {
			...mapState({
				menus: state => state.menus,
			}),
		},
		methods: {
			onInput(event) {
				// 触发事件，并传递输入框的值
				this.$emit('search', event.target.value);
			},
			newOrder() {
				this.$emit('newOrder')
			},
			// 查看详情
			detail(item) {
				uni.navigateTo({
					url: "/pages/order/newDetails/newDetails?item=" + encodeURIComponent(JSON.stringify(item))
				})
			},
			//一键退款
			yijiantuikuan(e) {
				var _this = this
				uni.showModal({
					content: '确定要一键退款吗？',
					title: "一键退款",
					success: function(res) {
						if (res.confirm) {
							_this.beg.request({
								url: _this.newPageUrl,
								method: 'post',
								data: {
									order_id: e
								}
							}).then(res => {
								if (res.code == 1) {
									_this.newOrder()
									uni.$u.toast(res.msg)
								} else {
									_this.newOrder()
									uni.$u.toast(res.msg)
								}
							})
						}
					}
				})
			},
			// 同意退款
			agreeBtn(val) {
				var _this = this
				uni.showModal({
					title: '提示',
					content: '确定同意退款吗？',
					success: function(res) {
						if (res.confirm) {
							_this.beg.request({
								url: '/index.php/admin/order.Order/return_agree',
								method: 'post',
								data: {
									order_id: val,
								}
							}).then(res => {
								if (res.code == 1) {
									_this.newOrder()
									uni.$u.toast(res.msg)
								} else {
									_this.newOrder()
									uni.$u.toast(res.msg)
								}
							})
						}
					}
				})
			},
			// 拒绝退款
			refuseBtn(val) {
				var _this = this
				uni.showModal({
					title: '提示',
					editable: true,
					placeholderText: '请填写拒绝原因',
					success: function(res) {
						console.log(res)
						if (res.content == '') {
							uni.showToast({
								icon: 'none',
								title: '请填写拒绝原因'
							})
							return
						}
						if (res.confirm) {
							_this.beg.request({
								url: '/index.php/admin/order.Order/return_refuse',
								method: 'post',
								data: {
									order_id: val,
									return_refuse_msg: res.content
								}
							}).then(res => {
								if (res.code == 1) {
									_this.newOrder()
									uni.$u.toast(res.msg)
								} else {
									_this.newOrder()
									uni.$u.toast(res.msg)
								}
							})
						}
					}
				})
			},
			formatDate(time) {
				let dt = new Date(time)
				let year = dt.getFullYear()
				let month = (dt.getMonth() + 1).toString().padStart(2, '0')
				let date = dt.getDate().toString().padStart(2, '0')
				let hour = dt.getHours().toString().padStart(2, '0')
				let minute = dt.getMinutes().toString().padStart(2, '0')
				let second = dt.getSeconds().toString().padStart(2, '0')
				return `${year}-${month}-${date} ${hour}:${minute}:${second}`
			},
			get_dy_settle(val) {
				this.beg.request({
					url: '/admin/order.Order/query_dy_settle',
					data: {
						order_id: val.id
					}
				}).then(res => {
					if (res.code == 1) {
						uni.$u.toast(res.msg)
						if (res.data.data) {
							this.tableDataTi = res.data.data;
							console.log(this.tableDataTi);
							this.dialogVisibleTi = true
						}
					} else {
						uni.$u.toast(res.msg)
					}
				})
			},
			dakai(val) {
				this.tableDataTi = [];
				this.get_dy_settle(val)
			},
			jiesuan(val) {
				uni.showModal({
					title: '提示',
					content: '确定手动结算吗？',
					success: (res) => {
						if (res.confirm) {
							this.beg.request({
									url: '/admin/order.Order/order_settle',
									method: 'post',
									data: {
										id: val.id
									}
								}).then((res) => {
									if (res.code == 1) {
										this.newOrder()
										uni.$u.toast(res.msg)
									} else {
										uni.$u.toast(res.msg)
									}
								})
								.catch((error) => {
									console.log('获取订单出错')
								})
						}
					}
				})
			},
			editStatus(type, val) {
				let title = ''
				if (type == 1) {
					title = '确定商家接单？（手动更改状态是为了处理异常，请仔细确认）'
				} else if (type == 300) {
					title = '确定已配送？（手动更改状态是为了处理异常，请仔细确认）'
				} else if (type == 500) {
					title = '确定配送完成？（手动更改状态是为了处理异常，请仔细确认）'
				}
				uni.showModal({
					title: '提示',
					content: title,
					success: (res) => {
						if (res.confirm) {
							this.beg.request({
									url: '/admin/order.Order/taking_order',
									method: 'post',
									data: {
										order_id: val.id,
										status: type
									}
								}).then((res) => {
									if (res.code == 1) {
										this.newOrder()
										uni.$u.toast(res.msg)
									} else {
										uni.$u.toast(res.msg)
									}
								})
								.catch((error) => {})
						}
					}
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.order-info {
		display: flex;
		align-items: center;
		margin-top: 7rpx;
	}

	.footer {
		display: flex;
		justify-content: flex-start;
		align-items: center;

		.detail {
			width: 125rpx;
			height: 55rpx;
			border-radius: 8rpx;
			background-color: #fdda34;
			color: #000;
			font-weight: 600;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.detail:not(:first-child) {
			margin-left: 30rpx;
		}
	}

	.order-name {
		font-size: 30rpx;
		font-weight: 600;
	}

	.setting {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-items: center;

		.detail {
			width: 125rpx;
			height: 55rpx;
			border-radius: 8rpx;
			background-color: #fdda34;
			color: #000;
			font-weight: 600;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.detail:not(:first-child) {
			margin-left: 30rpx;
		}
	}
</style>